<?php
require('main.php');

class Page extends Main
{
	function Page()
	{
		parent::Main();
		$this->database();
		$this->lib("message");
		$this->lib("pagination");
	}
	
	function show()
	{
		
	    $this->my_view("manage/page/page",$this->pageList() );
	}
	
	function init_add()
	{
		$this->my_view("manage/page/page_add",$this->languageList() );
		
	}
	function add()
	{
		$result = $this->db->insert('page',$this->_fetch_form());
		if($result){
			
			$this->message->set("恭喜你,添加成功！");
			$this->my_view("manage/page/page_success");
		}
		else{
			
			$this->message->set("抱歉,添加失败！");
			$this->my_view("manage/page/page_add",$this->languageList() );
			
		}
	
	}
	
	
	function delete()
	{
	   $id_sql = join(",",$_POST['id'] );
	   $result = $this->db->delete('page',"id in($id_sql)");
	   if($result){
	   	     $this->message->set("恭喜你,删除成功！");
	   	     $this->my_view("manage/page/page",$this->pageList() );
	   }
	   else{
	   	     $this->message->set("抱歉,删除失败！");
	   	     $this->my_view("manage/page/page",$this->pageList() ); 
	   }	
		
	}
	
	/**
	 * 
	 * 
	 * 修改
	 * @return unknown_type
	 */
	function init_update($id)
	{
		$language_list = $this->db->find("select * from languages"); 
		$page_id_row =  $this->db->find("select * from page where id= $id" ); 
		$data = array(
		'language_list' =>$language_list,
		'page_id_row'   =>$page_id_row
		);
		
		$this->my_view("manage/page/page_update",$data );
		
	}
	function update()
	{   
		$language_list = $this->db->find("select * from languages"); 
		$page_id_row =  $this->db->find("select * from page where id=".$_POST['id'] ); 
		$data = array(
		'language_list' =>$language_list,
		'page_id_row'   =>$page_id_row
		);
		
		
		$result = $this->db->update('page',$this->_fetch_form(),"id=".$_POST['id'] );
		
		if($result ){
			 $this->message->set("恭喜你,修改成功！");
			 $this->my_view('manage/page/page',$this->pageList() ); 
		}
		else{
			
			 $this->message->set("抱歉,修改失败！");
		     $this->my_view('manage/page/page_update',$data); 
		}		
	}
	
	/**
	 * 
	 * page页面列表的查询；
	 * 
	 * 
	 */
	function pageList()
	{   $total = $this->db->count('page');
	    $config = array(
	    'total_row' => $total
	    );
	    $this->pagination->init($config);
	    
		$page_list = $this->db->find("select * from page order by id desc limit "
		            .$this->pagination->start_row.",10");
		
		return  array(
		'page_list'=>$page_list
		);
		
	}
	
	/**
	 * 
	 * 语言列表
	 * 
	 */
	function languageList()
	{
		
		$language_list = $this->db->find("select * from languages");
		return array(
		'language_list'=>$language_list
		
		);
	}
	
	/**
	 * 
	 * 插入语句
	 * @return unknown_type
	 */
	
	function _fetch_form()
	{
		return array(
		'language'   => $_POST['language'],
		'name'    	 => $_POST['name'],
		'title'   	 => $_POST['title'],
		'url_pattern'=> $_POST['url_pattern'],
		'content'    => $_POST['content'],
		'keywords'   => $_POST['keywords'],
		'description'=> $_POST['description']
		);
		
	}
	
	
	
	
	
	
}
?>